package com.geek.mysql7work.work2.datasource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 主从数据库配置：1主2从
 *
 * @author lw
 */
@Configuration
public class DataSourceConfig {

    @Autowired
    Environment env;

    @Primary
    @Bean(name = "master")
    public DataSource masterDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("master.driver"));
        dataSource.setUrl(env.getProperty("master.url"));
        dataSource.setUsername(env.getProperty("master.user"));
        dataSource.setPassword(env.getProperty("master.password"));
        return dataSource;

    }

    @Bean(name = "slave1")
    public DataSource slave1DataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("slave1.driver"));
        dataSource.setUrl(env.getProperty("slave1.url"));
        dataSource.setUsername(env.getProperty("slave1.user"));
        dataSource.setPassword(env.getProperty("slave1.password"));
        return dataSource;
    }

    @Bean(name = "slave2")
    public DataSource slave2DataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("slave2.driver"));
        dataSource.setUrl(env.getProperty("slave2.url"));
        dataSource.setUsername(env.getProperty("slave2.user"));
        dataSource.setPassword(env.getProperty("slave2.password"));
        return dataSource;
    }
}
